﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SmokinAces.Object_Classes.Objects
{
    public enum Item_Type { Money = 1,Health = 2, Mana = 3 };
    public class Item : Game_Object, Draw_Object
    {
        //------this is information for the editor...
        static Type[] constructor_parameters = { typeof(int), typeof(int), typeof(Item_Type), typeof(Main_Engine) };
        static object[] default_const_values = { 0, 0, Item_Type.Money, null };
        //------------------------------------
        int ItemIndex;
        private Sort_Delegate drawSortDelegate;
        VertexPositionColorTexture[] baseImage;
		VertexPositionColorTexture[] vertecies;
		int[] indecies;
        public int increase = 10;
        private Item_Type _currentItem;
        public Item_Type currentItem
        {
            get { return _currentItem; }
            set
            {
                _currentItem = value;
                this.ItemIndex = parent.register_texture("Item\\Item" + ((int)_currentItem) + ".png");
            }
        }
        public Item(XmlReader xmlReader, Main_Engine Parent)
        {
            xmlReader.Read();
            string sLeft = xmlReader.ReadElementString("Left");
            string sTop = xmlReader.ReadElementString("Top");
            string sItemType = xmlReader.ReadElementString("Item_Type");
            //------------------------------------
            int left = Convert.ToInt32(sLeft);
            int top = Convert.ToInt32(sTop);
            Item_Type ItemType = (Item_Type)Enum.Parse(typeof(Item_Type), sItemType);
            Initialize(left, top, left + 20, top + 20, ItemType, Parent);
        }
        public override bool Save_XML(XmlTextWriter textWriter)
        {
            try
            {
                textWriter.WriteStartElement("Game_Object");
                textWriter.WriteAttributeString("Type", this.GetType().AssemblyQualifiedName);
                textWriter.WriteElementString("Left", baseImage[0].Position.X.ToString());
                textWriter.WriteElementString("Top", baseImage[0].Position.Y.ToString());
                textWriter.WriteElementString("Item_Type", Enum.GetName(typeof(Item_Type), _currentItem));
                textWriter.WriteEndElement();
                textWriter.Flush();
            }
            catch
            {
                return false;
            }
            return true;
        }

        public Item(int left, int top, Main_Engine parent)
        {
            Initialize(left, top, left + 20, top + 20, Item_Type.Money, parent);
        }
        public Item(int left, int top, Item_Type Item, Main_Engine parent)
        {
            Initialize(left, top, left + 20, top + 20, Item, parent);
        }
        private void Initialize(int left, int top, int right, int bottom, Item_Type Item, Main_Engine parent)
        {
            const float zorder = 1.0f;
            this.parent = parent;
            GraphicBB = new BoundingBox(new Vector3(left, top, Util.MinZ), new Vector3(right, bottom, Util.MaxZ));
            this.currentItem = Item;
            Color baseColor = new Color(255,255,255,255);
            //color = baseColor;
            baseImage = new VertexPositionColorTexture[4];
            baseImage[0].Position = new Vector3(left, top, zorder);
            baseImage[0].Color = baseColor;
            baseImage[0].TextureCoordinate = new Vector2(0.99f, 0.99f);
            baseImage[1].Position = new Vector3(left, bottom, zorder);
            baseImage[1].Color = baseColor;
            baseImage[1].TextureCoordinate = new Vector2(0.99f, 0.02f);
            baseImage[2].Position = new Vector3(right, top, zorder);
            baseImage[2].Color = baseColor;
            baseImage[2].TextureCoordinate = new Vector2(0.02f, 0.99f);
            baseImage[3].Position = new Vector3(right, bottom, zorder);
            baseImage[3].Color = baseColor;
            baseImage[3].TextureCoordinate = new Vector2(0.02f, 0.02f);

			vertecies = Util.ComplexMesh(baseImage, out indecies, 4, 4);
            Bounding_Regions.Add(new BoundingBox(new Vector3(left, top, -1), new Vector3(left + 20, top + 20, 1)));
            drawSortDelegate = new Sort_Delegate(((1024 - top) * 1024) + left + 0.9f, Draw);
        }

        #region Draw_Object Members

        public void Draw(GameTime gameTime, GraphicsDevice device)
        {
            
                //---Set the Texture for this layer
            parent.effect.BaseTexture = parent.Texture_List[ItemIndex];
                //---------Begin the drawing passes
            parent.effect.Begin();
            foreach (EffectPass pass in parent.effect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    //device.DrawUserPrimitives(PrimitiveType.TriangleStrip, baseImage, 0, 2);
					device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertecies, 0, vertecies.Length, indecies, 0, indecies.Length/3);
                    pass.End();
                }
            parent.effect.End();
        }
        public bool startDraw()
        {
            parent.Draw_Functions[3].Add(drawSortDelegate);
            return true;
        }
        public bool stopDraw()
        {
            parent.Draw_Functions[3].Remove(drawSortDelegate);
            return true;
        }
        #endregion

        #region Game_Object Members

        public override bool dispose()
        {
            //----------------I used some basic boolean logic here...
            stopDraw();
            return parent.Draw_Functions[3].Remove(drawSortDelegate);
            //return parent.remove_Drawing_Object("Item\\Item" + ((int)currentItem) + ".png", Draw, Layers.Layer3);
        }
        #endregion

    }
}
